package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.qlsl.androiddesign.kuaiwa.R;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import qlsl.androiddesign.activity.subactivity.ExChangeOrderActivity;
import qlsl.androiddesign.activity.subactivity.OrderActivity;
import qlsl.androiddesign.activity.subactivity.PostingShoppingDetailActivity;
import qlsl.androiddesign.activity.subactivity.RefundActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingCarActivity;
import qlsl.androiddesign.activity.subactivity.ShoppingDetailActivity;
import qlsl.androiddesign.adapter.subadapter.ShoppingDetailAdapter;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.entity.commonentity.City;
import qlsl.androiddesign.entity.commonentity.FragmentArgs;
import qlsl.androiddesign.entity.otherentity.GoodsList;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail;
import qlsl.androiddesign.entity.otherentity.ShoppingDetail.GoodsphotolistBean;
import qlsl.androiddesign.http.service.subservice.ShoppingDetailService;
import qlsl.androiddesign.util.commonutil.AdapterViewUtils;
import qlsl.androiddesign.util.commonutil.TextSizeUtils;
import qlsl.androiddesign.util.runfeng.AbJsonUtil;
import qlsl.androiddesign.util.runfeng.StringUtils;
import qlsl.androiddesign.util.runfeng.ToActivityUtil;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.ChildViewPager;
import qlsl.androiddesign.view.commonview.ChildViewPager.OnSingleTouchListener;
import qlsl.androiddesign.view.indicatorview.AutoPlayManager;
import qlsl.androiddesign.view.indicatorview.ImageIndicatorView.OnItemClickListener;
import qlsl.androiddesign.view.indicatorview.IndicatorView;

/**
 * 查看某个兑换商品页<br/>
 * 需要传入的键：data<br/>
 * 传入的值类型： ShoppingDetail<br/>
 * 传入的值含义：商品详情<br/>
 * 是否必传 ：是
 */
public class PostingShoppingDetailView extends
		FunctionView<PostingShoppingDetailActivity> implements
		OnItemClickListener, OnSingleTouchListener {

	private IndicatorView indicatorView;

	private AutoPlayManager autoBrocastManager;

	private ListView listView;

	private ShoppingDetail item;

	public PostingShoppingDetailView(PostingShoppingDetailActivity activity) {
		super(activity);
		setContentView(R.layout.activity_shopping_posting_detail);
	}

	protected void initView(View view) {
		item = (ShoppingDetail) activity.getIntent().getSerializableExtra(
				"data");
		setTitle("商品详情");
		listView = findViewById(R.id.listView);
		indicatorView = findViewById(R.id.net_indicate_view);
		indicatorView.setIndicateStyle(IndicatorView.INDICATE_ROUND_STYLE);
		indicatorView.setupLayoutByDefaultResource(1);
		indicatorView.show();
		((TextView) findViewById(R.id.color_name)).setText("商品详情");

		setRightButtonResource(R.drawable.details_shopping);
		showRightButton();
	}

	protected void initData() {
		ShoppingDetailService.queryShoppingDetai(item.getGoodsid(), this,
				activity);
		startHomeImageManager();
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		viewPager.setIntercept(true);
		viewPager.setOnSingleTouchListener(this);
	}

	private void startHomeImageManager() {
		autoBrocastManager = new AutoPlayManager(indicatorView);
		autoBrocastManager.setBroadcastEnable(true);
		autoBrocastManager.setBroadCastTimes(Integer.MAX_VALUE);
		autoBrocastManager.setBroadcastTimeIntevel(6 * 1000, 6 * 1000);
		autoBrocastManager.loop();
	}

	protected void initListener() {
		indicatorView.setOnItemClickListener(this);
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof ShoppingDetail) {
			ShoppingDetail shopp_result = (ShoppingDetail) t[0];
			shopp_result.setGoodsid(item.getGoodsid());
			item = shopp_result;
			setListViewData();
			setHomeImages();
			setDetail();
		}
	}

	public <T> void showNoData(T... t) {

	}

	private void setHomeImages() {
		indicatorView.setupLayout(getPhotoList());
		indicatorView.show();
	}

	private ArrayList<String> getPhotoList() {
		ArrayList<String> urls = new ArrayList<String>();
		List<GoodsphotolistBean> list = item.getGoodsphotolist();
		if (list.size() > 0) {
			for (GoodsphotolistBean exhibition : list) {
				urls.add(exhibition.getGoodsphoto());
			}
		} else {
			urls.add(item.getPhoto());
		}
		return urls;
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_right:// 购物车
			startActivity(ShoppingCarActivity.class);
			break;
		case R.id.goumai:
			doClickStartOrderView(view);
			break;
		case R.id.contact:
			doClickPhoneNumberView(view);
			break;
		case R.id.refund:
			doClickRefund();
			break;
		}

	}

	protected void doClickRefund() {
		FragmentArgs args = new FragmentArgs();
		args.add("name", "退款须知");
		args.add("url", "file:///android_asset/other/agreement.html");
		ToActivityUtil.toNextActivity(activity, RefundActivity.class, "data",
				args);
		// startActivity(RefundActivity.class);// 退货须知
	}

	/**
	 * 跳转到订单页
	 * 
	 * @param view
	 */
	private void doClickStartOrderView(View view) {
		FragmentArgs fragmentArgs2 = new FragmentArgs();
		List<GoodsList> aa = new ArrayList();
		GoodsList g = new GoodsList();
		g.setGoodsinfoid("" + item.getGoodsid());
		g.setNums("1");// 商品的数量
		aa.add(g);

		fragmentArgs2.add("goodslist", AbJsonUtil.toJson(aa));// 订单信息
		fragmentArgs2.add("totalprice", item.getPoints());// 商品总积分

		fragmentArgs2.add("goodsid", item.getGoodsid());// 商品id

		ToActivityUtil.toNextActivity(activity, ExChangeOrderActivity.class,
				"goumainBundle", fragmentArgs2);
	}

	/**
	 * 拨打商家电话
	 * 
	 * @param view
	 */
	private void doClickPhoneNumberView(View view) {
		if (!TextUtils.isEmpty(item.getBusinessmobile())) {
			ToActivityUtil
					.callPhoneActivity(activity, item.getBusinessmobile());
		} else {
			showToast("商家未提供电话号码");
		}

	}

	public TextView getTextViewNumber(View view) {
		TextView tv_num = (TextView) ((ViewGroup) view.getParent())
				.getChildAt(2);
		return tv_num;
	}

	private void setListViewData() {
		String[] names = new String[] { "品牌", "适配机型", "产地", "新旧", "产品编号" };
		String[] contents = new String[] { item.getBrands(), item.getAdapter(),
				item.getHomeofclaret(), item.getDegree(), item.getProductno() };

		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		contents = StringUtils.convertToStringArray(contents);

		for (int index = 0; index < contents.length; index++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", names[index]);
			map.put("content", contents[index]);
			list.add(map);
		}
		BaseAdapter adapter = new ShoppingDetailAdapter(activity, list);
		listView.setAdapter(adapter);
		AdapterViewUtils.setListViewHeight(listView);
	}

	private void setDetail() {
		((TextView) findViewById(R.id.goodname)).setText(""
				+ item.getGoodsname());
		((TextView) findViewById(R.id.goodnewprice1)).setText(""
				+ item.getPoints() + "挖币");
	}

	public void onActivityResult(int requestCode, int arg1,
			android.content.Intent data) {
		// if (arg0 == 1) {
		if (hasRefresh(data)) {
			setDetail();
			showToast("hasRefresh");
		}
		// }

		if (requestCode == Constant.SELECT_CITY_RESULT_CODE) {
			if (data != null) {
				City city = (City) data.getExtras().get("city");
				City city2 = (City) data.getExtras().get("city2");

				StringBuffer append = new StringBuffer();
				if (city2 != null) {
					append.append(city2.getArea_name());
				}
				if (city != null) {
					append.append(city.getArea_name());
					showToast("当前选择城市" + append);
				}
			}
		}

	}

	public void OnItemClick(View view, int position) {
		startActivityToPhotoBrowseActivity(position, item.getGoodsname(),
				getPhotoList());
	}

	public void onSingleTouch() {
		ChildViewPager viewPager = findViewById(R.id.viewpager_image);
		int position = viewPager.getCurrentItem();
		OnItemClick(null, position);
	}

}
